package com.xujiang.common_plus.util;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.xujiang.common_core.util.mcallback.Callback2;
import com.xujiang.common_core.util.tool.HandlerUtils;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * @author xujiang
 * @version 2017-08-08 11:43
 */
public class GlideUtils {

    @DrawableRes public int mDefaultError;
    @DrawableRes public int mDefaultLoading;

    public int getDefaultError() {
        return mDefaultError;
    }

    public GlideUtils setDefaultError(int mDefaultError) {
        this.mDefaultError = mDefaultError;
        return this;
    }

    public int getmDefaultLoading() {
        return mDefaultLoading;
    }

    public GlideUtils setmDefaultLoading(int mDefaultLoading) {
        this.mDefaultLoading = mDefaultLoading;
        return this;
    }

    private GlideUtils() {
    }

    private static GlideUtils glideUtils;

    public static GlideUtils get() {
        if (glideUtils == null) {
            glideUtils = new GlideUtils();
        }
        return glideUtils;
    }

    public boolean isEmpty(String url, ImageView imageView, @DrawableRes int defaultError) {
        if (TextUtils.isEmpty(url)) {
            imageView.setImageResource(defaultError);
            return true;
        }
        return false;
    }
    //
    //public <T> DrawableTypeRequest<T> loadPlaceError(DrawableTypeRequest<T> request,
    //        @DrawableRes int defaultError,
    //        @DrawableRes int defaultLoading) {
    //    return (DrawableTypeRequest<T>) request.error(defaultError).placeholder(defaultLoading);
    //}
    //
    //
    //public <T> DrawableTypeRequest<T> loadPlaceError(DrawableTypeRequest<T> request,
    //        @DrawableRes int defaultRes) {
    //    return (DrawableTypeRequest<T>) request.error(defaultRes).placeholder(defaultRes);
    //}
    //
    //public <T> DrawableTypeRequest<T> loadPlaceError(DrawableTypeRequest<T> request) {
    //    return loadPlaceError(request, mDefaultError, mDefaultLoading);
    //}
    //
    //public <T> DrawableTypeRequest<T> loadError(DrawableTypeRequest<T> request) {
    //    return loadError(request, mDefaultError);
    //}
    //
    //public <T> DrawableTypeRequest<T> loadError(DrawableTypeRequest<T> request,
    //        @DrawableRes int defaultLoading) {
    //    return (DrawableTypeRequest<T>) request.error(defaultLoading);
    //}
    //
    //public <T> DrawableTypeRequest<T> loadNoCache(DrawableTypeRequest<T> request) {
    //    return (DrawableTypeRequest<T>) request.diskCacheStrategy(DiskCacheStrategy.NONE)
    //            .skipMemoryCache(true);
    //}
    //
    //public <T> void bitmapImageViewTarget(DrawableTypeRequest<T> request, ImageView imageView,
    //        final Callback2<Bitmap> listener) {
    //    request.asBitmap().into(new BitmapImageViewTarget(imageView) {
    //        @Override public void onStart() {
    //            listener.onPreExecute();
    //        }
    //
    //        @Override public void onStop() {
    //            listener.onPostExecute();
    //        }
    //
    //        @Override public void onResourceReady(Bitmap resource,
    //                GlideAnimation<? super Bitmap> glideAnimation) {
    //            super.onResourceReady(resource, glideAnimation);
    //            listener.onSuccess(null);
    //        }
    //    });
    //}

    public void clearCache(Context context, final Callback2<Object> callback) {
        Subscriber<String> subscriber = new Subscriber<String>() {
            @Override
            public void onStart() {
                HandlerUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        callback.onPreExecute();
                    }
                });
            }

            @Override
            public void onCompleted() {
                callback.onFinish();
            }

            @Override
            public void onError(Throwable e) {
                callback.onPostExecute();
                callback.onError(e);
            }

            @Override
            public void onNext(String o) {
                callback.onPostExecute();
                callback.onSuccess(o);
            }
        };
        Observable.just(context).observeOn(Schedulers.io()).map(new Func1<Context, Context>() {
            @Override
            public Context call(Context context) {
                Glide.get(context).clearDiskCache();
                return context;
            }
        }).observeOn(AndroidSchedulers.mainThread()).map(new Func1<Context, String>() {
            @Override
            public String call(Context context) {
                Glide.get(context).clearMemory();
                return "ok";
            }
        }).subscribe(subscriber);
    }

}
